/*
 * DatabaseConfig.java
 */

package nxtgeninventorysystem.View.Dialog;

import javax.swing.WindowConstants;
import nxtgeninventorysystem.Controller.Actions;
import nxtgeninventorysystem.Model.Database;
import nxtgeninventorysystem.View.Gui;
import org.jdesktop.application.Action;

/**
 * The dialog box that displays an interface for connecting to the database.
 * Member of the MVC View Module (Gui).
 * @author Seraph Softworks
 */
public class DatabaseConfig extends GuiDialog {

    /** Creates new form DatabaseConfig */
    public DatabaseConfig(java.awt.Frame parent) {
        super(parent);
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    }

    @Override
    public void resetFields() {
        Actions.database.reflectDatabaseFields();
    }

    @Override
    protected void initialize() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        saveButton = new javax.swing.JButton();
        defaults = new javax.swing.JButton();
        closeButton = new javax.swing.JButton();
        applyButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();
        javax.swing.JLabel label_hostname = new javax.swing.JLabel();
        javax.swing.JLabel label_portno = new javax.swing.JLabel();
        javax.swing.JLabel label_dbname = new javax.swing.JLabel();
        javax.swing.JLabel label_username = new javax.swing.JLabel();
        javax.swing.JLabel label_password = new javax.swing.JLabel();
        info_hostname = new javax.swing.JTextField();
        info_port = new javax.swing.JSpinner();
        info_database = new javax.swing.JTextField();
        info_username = new javax.swing.JTextField();
        info_password = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(nxtgeninventorysystem.Driver.class).getContext().getResourceMap(DatabaseConfig.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setMinimumSize(new java.awt.Dimension(401, 198));
        setName("Form"); // NOI18N
        setResizable(false);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(nxtgeninventorysystem.Driver.class).getContext().getActionMap(DatabaseConfig.class, this);
        saveButton.setAction(actionMap.get("save")); // NOI18N
        saveButton.setText(resourceMap.getString("saveButton.text")); // NOI18N
        saveButton.setName("saveButton"); // NOI18N

        defaults.setAction(actionMap.get("restore_defaults")); // NOI18N
        defaults.setText(resourceMap.getString("defaults.text")); // NOI18N
        defaults.setName("defaults"); // NOI18N

        closeButton.setAction(actionMap.get("close")); // NOI18N
        closeButton.setText(resourceMap.getString("closeButton.text")); // NOI18N
        closeButton.setName("closeButton"); // NOI18N

        applyButton.setAction(actionMap.get("apply")); // NOI18N
        applyButton.setText(resourceMap.getString("applyButton.text")); // NOI18N
        applyButton.setName("applyButton"); // NOI18N

        okButton.setAction(actionMap.get("ok")); // NOI18N
        okButton.setText(resourceMap.getString("okButton.text")); // NOI18N
        okButton.setName("okButton"); // NOI18N

        label_hostname.setText(resourceMap.getString("label_hostname.text")); // NOI18N
        label_hostname.setName("label_hostname"); // NOI18N

        label_portno.setText(resourceMap.getString("label_portno.text")); // NOI18N
        label_portno.setName("label_portno"); // NOI18N

        label_dbname.setText(resourceMap.getString("label_dbname.text")); // NOI18N
        label_dbname.setName("label_dbname"); // NOI18N

        label_username.setText(resourceMap.getString("label_username.text")); // NOI18N
        label_username.setName("label_username"); // NOI18N

        label_password.setText(resourceMap.getString("label_password.text")); // NOI18N
        label_password.setName("label_password"); // NOI18N

        info_hostname.setText(resourceMap.getString("info_hostname.text")); // NOI18N
        info_hostname.setName("info_hostname"); // NOI18N

        info_port.setName("info_port"); // NOI18N
        info_port.setValue(3306);

        info_database.setText(resourceMap.getString("info_database.text")); // NOI18N
        info_database.setName("info_database"); // NOI18N

        info_username.setText(resourceMap.getString("info_username.text")); // NOI18N
        info_username.setName("info_username"); // NOI18N

        info_password.setText(resourceMap.getString("info_password.text")); // NOI18N
        info_password.setName("info_password"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(label_password)
                        .addComponent(label_username)
                        .addComponent(label_dbname)
                        .addComponent(label_portno)
                        .addComponent(label_hostname))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(saveButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(defaults)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(info_port, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE)
                    .addComponent(info_database, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE)
                    .addComponent(info_password, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE)
                    .addComponent(info_username, javax.swing.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE)
                    .addComponent(info_hostname, javax.swing.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(okButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(applyButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(closeButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label_hostname)
                    .addComponent(info_hostname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label_portno)
                    .addComponent(info_port, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label_dbname)
                    .addComponent(info_database, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label_username)
                    .addComponent(info_username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label_password)
                    .addComponent(info_password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saveButton)
                    .addComponent(closeButton)
                    .addComponent(applyButton)
                    .addComponent(okButton)
                    .addComponent(defaults))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    @Action
    public void close() {
        Thread wait = new Thread() {
            public void run() {
                while(isConnecting()) {

                }
                Gui.setBusyEffect(false);

                if(!(Database.isConnected())) {
                    Gui.setStatusBarMessage("Cannot continue until connected to database...");
                    return;
                }

                Gui.setStatusBarMessage("Ready...");
                dispose();
            }
        };
        wait.start();
    }

    @Action
    public void apply() {
        Actions.database.connectToDatabase();
    }

    @Action
    public void ok() {
        apply();
        close();
    }

    @Action
    public void save() {
        Actions.database.saveDatabaseConfig();
    }

    @Action
    public void restore_defaults() {
        info_hostname.setText("localhost");
        info_port.setValue(new Integer(3306));
        info_database.setText("nxtgen_inventory");
        info_username.setText("root");
        info_password.setText("12345");
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton applyButton;
    private javax.swing.JButton closeButton;
    private javax.swing.JButton defaults;
    private javax.swing.JTextField info_database;
    private javax.swing.JTextField info_hostname;
    private javax.swing.JPasswordField info_password;
    private javax.swing.JSpinner info_port;
    private javax.swing.JTextField info_username;
    private javax.swing.JButton okButton;
    private javax.swing.JButton saveButton;
    // End of variables declaration//GEN-END:variables

    private boolean connecting = false;

    /**
     * @return the connecting
     */
    public boolean isConnecting() {
        return connecting;
    }

    /**
     * @param connecting the connecting to set
     */
    public void setConnecting(boolean connecting) {
        this.connecting = connecting;
    }

    public String getDatabase() {
        return info_database.getText();
    }

    public void setDatabase(String t) {
        info_database.setText(t);
    }

    public String getHostname() {
        return info_hostname.getText();
    }

    public void setHostname(String t) {
        info_hostname.setText(t);
    }

    public String getPassword() {
        return String.copyValueOf(info_password.getPassword());
    }

    public void setPassword(String t) {
        info_password.setText(t);
    }

    public String getPort() {
        return "" + info_port.getValue();
    }

    public void setPort(String t) {
        info_port.setValue(Integer.valueOf(t));
    }

    public String getUsername() {
        return info_username.getText();
    }

    public void setUsername(String t) {
        info_username.setText(t);
    }

}
